package l1j.server.server.clientpackets;

import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.假人系統.DummyThread;
import l1j.server.Config;
import l1j.server.echo.ClientThread;
import l1j.server.server.FishingTimeController;
import l1j.server.server.GetNowTime;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.skill.L1SkillId;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.serverpackets.S_WhoAmount;
import l1j.server.server.serverpackets.S_WhoCharinfo;
import l1j.william.L1GameReStart;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來查詢線上人數的封包
 */
public class C_Who extends ClientBasePacket {
	private static final Logger _log = Logger.getLogger(C_Who.class.getName());
	private static final String C_WHO = "[C] C_Who";
	@Override
	public String getType() {
		return C_WHO;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null) {
				return;
			}

			final String s = this.readS();
			if(s == null){
				return;
			}
			final L1PcInstance find = L1World.getInstance().getPlayer(s);
			if (find != null && !find.hasSkillEffect(L1SkillId.GMSTATUS_WHISPER)) {
				pc.sendPackets(new S_WhoCharinfo(find));
			} else {
				if(DummyThread.getInstance().checkNameOnline(s)){
					pc.sendPackets(new S_WhoCharinfo(DummyThread.getInstance().getDummyTemp(s)));
				}else if (Config.ALT_WHO_COMMAND) {
					StringBuilder msg = new StringBuilder();

					final int Mon = GetNowTime.GetNowMonth();//TODO 月份錯誤補正
					final int second = L1GameReStart.getInstance().GetRemnant();
					msg.append("\\fV＊㊣＊＊◎《 伺服器配置資訊 》◎＊＊㊣＊\n【經驗值】: ").append(Config.RATE_XP)
					.append(" 倍\n【掉寶率】: ").append(Config.RATE_DROP_ITEMS).append(" 倍\n【取得金幣】: ").append(Config.RATE_DROP_ADENA).append(" 倍\n")
					.append("【衝武器】 ").append(Config.ENCHANT_CHANCE_WEAPON).append("%\n【衝防具】 ").append(Config.ENCHANT_CHANCE_ARMOR).append("%\n")
					//TODO 今天日期
					.append("\\fU今天是 ").append(GetNowTime.GetNowYear()).append(" 年 ").append(Mon + 1).append(" 月 ").append(GetNowTime.GetNowDay()).append(" 日。\n")
					//TODO 目前時間
					.append("\\fU現在時間(24h): ").append(GetNowTime.GetNowHour()).append(" 時 ").append(GetNowTime.GetNowMinute()).append(" 分 ").append(GetNowTime.GetNowSecond()).append(" 秒。\n")
					.append("\\fV★ 距離伺服器重開時間還有★\n")
					.append(second / 60 / 60 + " 小時 ").append( second / 60 % 60+ " 分 ").append(second % 60 + " 秒")
					.append("\n\\fV★★★★★★★★★★★★");

					//	if (Config.REST_TIME != 0) {}

					pc.sendPackets(new S_SystemMessage(msg.toString()));

					final int AllPcCount = L1World.getInstance().getAllPlayerSize()+FishingTimeController.getInstance().getGayPcCount()+L1World.getInstance().getBogusAmount();
					final String amount = String.valueOf(AllPcCount);
					//TODO 線上資訊
					S_WhoAmount s_whoamount = new S_WhoAmount(amount);
					pc.sendPackets(s_whoamount);

					s_whoamount = null;
					msg.setLength(1);
					msg = null;


				} else {
					final String amount = String.valueOf(L1World.getInstance().getAllPlayerSize());
					pc.sendPackets(new S_WhoAmount(amount));
				}
				// 對像居場合表示？方修正願。
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
